{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "RRYSu48huSUW",
        "outputId": "563df83b-ff47-42c9-b9d4-9d052886053f"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "\n",
            "[notice] A new release of pip is available: 23.1.2 -> 23.2\n",
            "[notice] To update, run: C:\\Users\\freestone\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\python.exe -m pip install --upgrade pip\n"
          ]
        }
      ],
      "source": [
        "!pip -q install langchain huggingface_hub openai google-search-results tiktoken cohere faiss-cpu"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "4rQcxx6M6Atf"
      },
      "source": [
        "# BabyAGI with LangChain\n",
        "\n",
        "Task Creation  \n",
        "Task Prioritization   \n",
        "Execution"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "dNA4TsHpu6OM"
      },
      "outputs": [],
      "source": [
        "import os\n",
        "\n",
        "os.environ[\"OPENAI_API_KEY\"] = \"\"\n",
        "# SERPAPI_API_KEY\n",
        "os.environ[\"SERPAPI_API_KEY\"] = \"\""
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "HqwsGJDhvAQ5"
      },
      "source": [
        "### Setting up BabyAGI"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "id": "lgesD0jrvDyG"
      },
      "outputs": [
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "C:\\Users\\freestone\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\deeplake\\util\\check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.11) is available. It's recommended that you update to the latest version using `pip install -U deeplake`.\n",
            "  warnings.warn(\n"
          ]
        }
      ],
      "source": [
        "import os\n",
        "from collections import deque\n",
        "from typing import Dict, List, Optional, Any\n",
        "\n",
        "from langchain import LLMChain, OpenAI, PromptTemplate\n",
        "from langchain.embeddings import OpenAIEmbeddings\n",
        "from langchain.llms import BaseLLM\n",
        "from langchain.vectorstores.base import VectorStore\n",
        "from pydantic import BaseModel, Field\n",
        "from langchain.chains.base import Chain"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "lNgWCWJf2I7K"
      },
      "outputs": [],
      "source": [
        "from langchain.vectorstores import FAISS\n",
        "from langchain.docstore import InMemoryDocstore"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "PlsfJFm32MHq"
      },
      "outputs": [],
      "source": [
        "# Define your embedding model\n",
        "embeddings_model = OpenAIEmbeddings()\n",
        "\n",
        "# Initialize the vectorstore as empty\n",
        "import faiss\n",
        "embedding_size = 1536\n",
        "index = faiss.IndexFlatL2(embedding_size)\n",
        "vectorstore = FAISS(embeddings_model.embed_query, index, InMemoryDocstore({}), {})"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "BgoHGnFX27u_"
      },
      "source": [
        "## Chains"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "id": "LVHdyOhw2orJ"
      },
      "outputs": [],
      "source": [
        "class TaskCreationChain(LLMChain):\n",
        "    \"\"\"Chain to generates tasks.\"\"\"\n",
        "\n",
        "    @classmethod\n",
        "    def from_llm(cls, llm: BaseLLM, verbose: bool = True) -> LLMChain:\n",
        "        \"\"\"Get the response parser.\"\"\"\n",
        "        task_creation_template = (\n",
        "            \"You are an task creation AI that uses the result of an execution agent\"\n",
        "            \" to create new tasks with the following objective: {objective},\"\n",
        "            \" The last completed task has the result: {result}.\"\n",
        "            \" This result was based on this task description: {task_description}.\"\n",
        "            \" These are incomplete tasks: {incomplete_tasks}.\"\n",
        "            \" Based on the result, create new tasks to be completed\"\n",
        "            \" by the AI system that do not overlap with incomplete tasks.\"\n",
        "            \" Return the tasks as an array.\"\n",
        "        )\n",
        "        prompt = PromptTemplate(\n",
        "            template=task_creation_template,\n",
        "            input_variables=[\"result\", \"task_description\", \"incomplete_tasks\", \"objective\"],\n",
        "        )\n",
        "        return cls(prompt=prompt, llm=llm, verbose=verbose)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "id": "IIww7Sh52lSy"
      },
      "outputs": [],
      "source": [
        "class TaskPrioritizationChain(LLMChain):\n",
        "    \"\"\"Chain to prioritize tasks.\"\"\"\n",
        "\n",
        "    @classmethod\n",
        "    def from_llm(cls, llm: BaseLLM, verbose: bool = True) -> LLMChain:\n",
        "        \"\"\"Get the response parser.\"\"\"\n",
        "        task_prioritization_template = (\n",
        "            \"You are an task prioritization AI tasked with cleaning the formatting of and reprioritizing\"\n",
        "            \" the following tasks: {task_names}.\"\n",
        "            \" Consider the ultimate objective of your team: {objective}.\"\n",
        "            \" Do not remove any tasks. Return the result as a numbered list, like:\"\n",
        "            \" #. First task\"\n",
        "            \" #. Second task\"\n",
        "            \" Start the task list with number {next_task_id}.\"\n",
        "        )\n",
        "        prompt = PromptTemplate(\n",
        "            template=task_prioritization_template,\n",
        "            input_variables=[\"task_names\", \"next_task_id\", \"objective\"],\n",
        "        )\n",
        "        return cls(prompt=prompt, llm=llm, verbose=verbose)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "gdnuvKVb2xXo"
      },
      "outputs": [],
      "source": [
        "# class ExecutionChain(LLMChain):\n",
        "#     \"\"\"Chain to execute tasks.\"\"\"\n",
        "\n",
        "#     @classmethod\n",
        "#     def from_llm(cls, llm: BaseLLM, verbose: bool = True) -> LLMChain:\n",
        "#         \"\"\"Get the response parser.\"\"\"\n",
        "#         execution_template = (\n",
        "#             \"You are an AI who performs one task based on the following objective: {objective}.\"\n",
        "#             \" Take into account these previously completed tasks: {context}.\"\n",
        "#             \" Your task: {task}.\"\n",
        "#             \" Response:\"\n",
        "#         )\n",
        "#         prompt = PromptTemplate(\n",
        "#             template=execution_template,\n",
        "#             input_variables=[\"objective\", \"context\", \"task\"],\n",
        "#         )\n",
        "#         return cls(prompt=prompt, llm=llm, verbose=verbose)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Fnn09nrk8u1J"
      },
      "source": [
        "#### With tools"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "id": "nFvw4Lei8xFZ"
      },
      "outputs": [],
      "source": [
        "from langchain.agents import ZeroShotAgent, Tool, AgentExecutor\n",
        "from langchain import OpenAI, SerpAPIWrapper, LLMChain\n",
        "todo_prompt = PromptTemplate.from_template(\"You are a planner who is an expert at coming up with a todo list for a given objective. Come up with a todo list for this objective: {objective}\")\n",
        "todo_chain = LLMChain(llm=OpenAI(temperature=0), prompt=todo_prompt)\n",
        "search = SerpAPIWrapper()\n",
        "tools = [\n",
        "    Tool(\n",
        "        name = \"Search\",\n",
        "        func=search.run,\n",
        "        description=\"useful for when you need to answer questions about current events\"\n",
        "    ),\n",
        "    Tool(\n",
        "        name = \"TODO\",\n",
        "        func=todo_chain.run,\n",
        "        description=\"useful for when you need to come up with todo lists. Input: an objective to create a todo list for. Output: a todo list for that objective. Please be very clear what the objective is!\"\n",
        "    )\n",
        "]\n",
        "\n",
        "\n",
        "prefix = \"\"\"You are an AI who performs one task based on the following objective: {objective}. Take into account these previously completed tasks: {context}.\"\"\"\n",
        "suffix = \"\"\"Question: {task}\n",
        "{agent_scratchpad}\"\"\"\n",
        "prompt = ZeroShotAgent.create_prompt(\n",
        "    tools, \n",
        "    prefix=prefix, \n",
        "    suffix=suffix, \n",
        "    input_variables=[\"objective\", \"task\", \"context\",\"agent_scratchpad\"]\n",
        ")"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Q-5jlrxR21st"
      },
      "source": [
        "## BabyAGI Controller"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 13,
      "metadata": {
        "id": "gsfLtP3B23UU"
      },
      "outputs": [],
      "source": [
        "def get_next_task(task_creation_chain: LLMChain, result: Dict, task_description: str, task_list: List[str], objective: str) -> List[Dict]:\n",
        "    \"\"\"Get the next task.\"\"\"\n",
        "    incomplete_tasks = \", \".join(task_list)\n",
        "    response = task_creation_chain.run(result=result, task_description=task_description, incomplete_tasks=incomplete_tasks, objective=objective)\n",
        "    new_tasks = response.split('\\n')\n",
        "    return [{\"task_name\": task_name} for task_name in new_tasks if task_name.strip()]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 15,
      "metadata": {
        "id": "BaCq6W8P3ATv"
      },
      "outputs": [],
      "source": [
        "def prioritize_tasks(task_prioritization_chain: LLMChain, this_task_id: int, task_list: List[Dict], objective: str) -> List[Dict]:\n",
        "    \"\"\"Prioritize tasks.\"\"\"\n",
        "    task_names = [t[\"task_name\"] for t in task_list]\n",
        "    next_task_id = int(this_task_id) + 1\n",
        "    response = task_prioritization_chain.run(task_names=task_names, \n",
        "                                             next_task_id=next_task_id, \n",
        "                                             objective=objective)\n",
        "    new_tasks = response.split('\\n')\n",
        "    prioritized_task_list = []\n",
        "    for task_string in new_tasks:\n",
        "        if not task_string.strip():\n",
        "            continue\n",
        "        task_parts = task_string.strip().split(\".\", 1)\n",
        "        if len(task_parts) == 2:\n",
        "            task_id = task_parts[0].strip()\n",
        "            task_name = task_parts[1].strip()\n",
        "            prioritized_task_list.append({\"task_id\": task_id, \"task_name\": task_name})\n",
        "    return prioritized_task_list"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 16,
      "metadata": {
        "id": "dvHd5mGu3NTk"
      },
      "outputs": [],
      "source": [
        "def _get_top_tasks(vectorstore, query: str, k: int) -> List[str]:\n",
        "    \"\"\"Get the top k tasks based on the query.\"\"\"\n",
        "    results = vectorstore.similarity_search_with_score(query, k=k)\n",
        "    if not results:\n",
        "        return []\n",
        "    sorted_results, _ = zip(*sorted(results, key=lambda x: x[1], reverse=True))\n",
        "    return [str(item.metadata['task']) for item in sorted_results]\n",
        "\n",
        "def execute_task(vectorstore, execution_chain: LLMChain, objective: str, task: str, k: int = 5) -> str:\n",
        "    \"\"\"Execute a task.\"\"\"\n",
        "    context = _get_top_tasks(vectorstore, query=objective, k=k)\n",
        "    return execution_chain.run(objective=objective, context=context, task=task)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "uJ4SNyff3lXm"
      },
      "outputs": [],
      "source": [
        "# class BabyAGI(Chain, BaseModel):\n",
        "#     \"\"\"Controller model for the BabyAGI agent.\"\"\"\n",
        "\n",
        "#     task_list: deque = Field(default_factory=deque)\n",
        "#     task_creation_chain: TaskCreationChain = Field(...)\n",
        "#     task_prioritization_chain: TaskPrioritizationChain = Field(...)\n",
        "#     execution_chain: ExecutionChain = Field(...)\n",
        "#     task_id_counter: int = Field(1)\n",
        "#     vectorstore: VectorStore = Field(init=False)\n",
        "#     max_iterations: Optional[int] = None\n",
        "        \n",
        "#     class Config:\n",
        "#         \"\"\"Configuration for this pydantic object.\"\"\"\n",
        "#         arbitrary_types_allowed = True\n",
        "\n",
        "#     def add_task(self, task: Dict):\n",
        "#         self.task_list.append(task)\n",
        "\n",
        "#     def print_task_list(self):\n",
        "#         print(\"\\033[95m\\033[1m\" + \"\\n*****TASK LIST*****\\n\" + \"\\033[0m\\033[0m\")\n",
        "#         for t in self.task_list:\n",
        "#             print(str(t[\"task_id\"]) + \": \" + t[\"task_name\"])\n",
        "\n",
        "#     def print_next_task(self, task: Dict):\n",
        "#         print(\"\\033[92m\\033[1m\" + \"\\n*****NEXT TASK*****\\n\" + \"\\033[0m\\033[0m\")\n",
        "#         print(str(task[\"task_id\"]) + \": \" + task[\"task_name\"])\n",
        "\n",
        "#     def print_task_result(self, result: str):\n",
        "#         print(\"\\033[93m\\033[1m\" + \"\\n*****TASK RESULT*****\\n\" + \"\\033[0m\\033[0m\")\n",
        "#         print(result)\n",
        "        \n",
        "#     @property\n",
        "#     def input_keys(self) -> List[str]:\n",
        "#         return [\"objective\"]\n",
        "    \n",
        "#     @property\n",
        "#     def output_keys(self) -> List[str]:\n",
        "#         return []\n",
        "\n",
        "#     def _call(self, inputs: Dict[str, Any]) -> Dict[str, Any]:\n",
        "#         \"\"\"Run the agent.\"\"\"\n",
        "#         objective = inputs['objective']\n",
        "#         first_task = inputs.get(\"first_task\", \"Make a todo list\")\n",
        "#         self.add_task({\"task_id\": 1, \"task_name\": first_task})\n",
        "#         num_iters = 0\n",
        "#         while True:\n",
        "#             if self.task_list:\n",
        "#                 self.print_task_list()\n",
        "\n",
        "#                 # Step 1: Pull the first task\n",
        "#                 task = self.task_list.popleft()\n",
        "#                 self.print_next_task(task)\n",
        "\n",
        "#                 # Step 2: Execute the task\n",
        "#                 result = execute_task(\n",
        "#                     self.vectorstore, self.execution_chain, objective, task[\"task_name\"]\n",
        "#                 )\n",
        "#                 this_task_id = int(task[\"task_id\"])\n",
        "#                 self.print_task_result(result)\n",
        "\n",
        "#                 # Step 3: Store the result in Pinecone\n",
        "#                 result_id = f\"result_{task['task_id']}\"\n",
        "#                 self.vectorstore.add_texts(\n",
        "#                     texts=[result],\n",
        "#                     metadatas=[{\"task\": task[\"task_name\"]}],\n",
        "#                     ids=[result_id],\n",
        "#                 )\n",
        "\n",
        "#                 # Step 4: Create new tasks and reprioritize task list\n",
        "#                 new_tasks = get_next_task(\n",
        "#                     self.task_creation_chain, result, task[\"task_name\"], [t[\"task_name\"] for t in self.task_list], objective\n",
        "#                 )\n",
        "#                 for new_task in new_tasks:\n",
        "#                     self.task_id_counter += 1\n",
        "#                     new_task.update({\"task_id\": self.task_id_counter})\n",
        "#                     self.add_task(new_task)\n",
        "#                 self.task_list = deque(\n",
        "#                     prioritize_tasks(\n",
        "#                         self.task_prioritization_chain, this_task_id, list(self.task_list), objective\n",
        "#                     )\n",
        "#                 )\n",
        "#             num_iters += 1\n",
        "#             if self.max_iterations is not None and num_iters == self.max_iterations:\n",
        "#                 print(\"\\033[91m\\033[1m\" + \"\\n*****TASK ENDING*****\\n\" + \"\\033[0m\\033[0m\")\n",
        "#                 break\n",
        "#         return {}\n",
        "\n",
        "#     @classmethod\n",
        "#     def from_llm(\n",
        "#         cls,\n",
        "#         llm: BaseLLM,\n",
        "#         vectorstore: VectorStore,\n",
        "#         verbose: bool = False,\n",
        "#         **kwargs\n",
        "#     ) -> \"BabyAGI\":\n",
        "#         \"\"\"Initialize the BabyAGI Controller.\"\"\"\n",
        "#         task_creation_chain = TaskCreationChain.from_llm(\n",
        "#             llm, verbose=verbose\n",
        "#         )\n",
        "#         task_prioritization_chain = TaskPrioritizationChain.from_llm(\n",
        "#             llm, verbose=verbose\n",
        "#         )\n",
        "#         execution_chain = ExecutionChain.from_llm(llm, verbose=verbose)\n",
        "#         return cls(\n",
        "#             task_creation_chain=task_creation_chain,\n",
        "#             task_prioritization_chain=task_prioritization_chain,\n",
        "#             execution_chain=execution_chain,\n",
        "#             vectorstore=vectorstore,\n",
        "#             **kwargs\n",
        "#         )"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 17,
      "metadata": {
        "id": "ROryb6Fn9nsw"
      },
      "outputs": [],
      "source": [
        "class BabyAGI(Chain, BaseModel):\n",
        "    \"\"\"Controller model for the BabyAGI agent.\"\"\"\n",
        "\n",
        "    task_list: deque = Field(default_factory=deque)\n",
        "    task_creation_chain: TaskCreationChain = Field(...)\n",
        "    task_prioritization_chain: TaskPrioritizationChain = Field(...)\n",
        "    execution_chain: AgentExecutor = Field(...)\n",
        "    task_id_counter: int = Field(1)\n",
        "    vectorstore: VectorStore = Field(init=False)\n",
        "    max_iterations: Optional[int] = None\n",
        "        \n",
        "    class Config:\n",
        "        \"\"\"Configuration for this pydantic object.\"\"\"\n",
        "        arbitrary_types_allowed = True\n",
        "\n",
        "    def add_task(self, task: Dict):\n",
        "        self.task_list.append(task)\n",
        "\n",
        "    def print_task_list(self):\n",
        "        print(\"\\033[95m\\033[1m\" + \"\\n*****TASK LIST*****\\n\" + \"\\033[0m\\033[0m\")\n",
        "        for t in self.task_list:\n",
        "            print(str(t[\"task_id\"]) + \": \" + t[\"task_name\"])\n",
        "\n",
        "    def print_next_task(self, task: Dict):\n",
        "        print(\"\\033[92m\\033[1m\" + \"\\n*****NEXT TASK*****\\n\" + \"\\033[0m\\033[0m\")\n",
        "        print(str(task[\"task_id\"]) + \": \" + task[\"task_name\"])\n",
        "\n",
        "    def print_task_result(self, result: str):\n",
        "        print(\"\\033[93m\\033[1m\" + \"\\n*****TASK RESULT*****\\n\" + \"\\033[0m\\033[0m\")\n",
        "        print(result)\n",
        "        \n",
        "    @property\n",
        "    def input_keys(self) -> List[str]:\n",
        "        return [\"objective\"]\n",
        "    \n",
        "    @property\n",
        "    def output_keys(self) -> List[str]:\n",
        "        return []\n",
        "\n",
        "    def _call(self, inputs: Dict[str, Any]) -> Dict[str, Any]:\n",
        "        \"\"\"Run the agent.\"\"\"\n",
        "        objective = inputs['objective']\n",
        "        first_task = inputs.get(\"first_task\", \"Make a todo list\")\n",
        "        self.add_task({\"task_id\": 1, \"task_name\": first_task})\n",
        "        num_iters = 0\n",
        "        while True:\n",
        "            if self.task_list:\n",
        "                self.print_task_list()\n",
        "\n",
        "                # Step 1: Pull the first task\n",
        "                task = self.task_list.popleft()\n",
        "                self.print_next_task(task)\n",
        "\n",
        "                # Step 2: Execute the task\n",
        "                result = execute_task(\n",
        "                    self.vectorstore, self.execution_chain, objective, task[\"task_name\"]\n",
        "                )\n",
        "                this_task_id = int(task[\"task_id\"])\n",
        "                self.print_task_result(result)\n",
        "\n",
        "                # Step 3: Store the result in Pinecone\n",
        "                result_id = f\"result_{task['task_id']}\"\n",
        "                self.vectorstore.add_texts(\n",
        "                    texts=[result],\n",
        "                    metadatas=[{\"task\": task[\"task_name\"]}],\n",
        "                    ids=[result_id],\n",
        "                )\n",
        "\n",
        "                # Step 4: Create new tasks and reprioritize task list\n",
        "                new_tasks = get_next_task(\n",
        "                    self.task_creation_chain, result, task[\"task_name\"], [t[\"task_name\"] for t in self.task_list], objective\n",
        "                )\n",
        "                for new_task in new_tasks:\n",
        "                    self.task_id_counter += 1\n",
        "                    new_task.update({\"task_id\": self.task_id_counter})\n",
        "                    self.add_task(new_task)\n",
        "                self.task_list = deque(\n",
        "                    prioritize_tasks(\n",
        "                        self.task_prioritization_chain, this_task_id, list(self.task_list), objective\n",
        "                    )\n",
        "                )\n",
        "            num_iters += 1\n",
        "            if self.max_iterations is not None and num_iters == self.max_iterations:\n",
        "                print(\"\\033[91m\\033[1m\" + \"\\n*****TASK ENDING*****\\n\" + \"\\033[0m\\033[0m\")\n",
        "                break\n",
        "        return {}\n",
        "\n",
        "    @classmethod\n",
        "    def from_llm(\n",
        "        cls,\n",
        "        llm: BaseLLM,\n",
        "        vectorstore: VectorStore,\n",
        "        verbose: bool = False,\n",
        "        **kwargs\n",
        "    ) -> \"BabyAGI\":\n",
        "        \"\"\"Initialize the BabyAGI Controller.\"\"\"\n",
        "        task_creation_chain = TaskCreationChain.from_llm(\n",
        "            llm, verbose=verbose\n",
        "        )\n",
        "        task_prioritization_chain = TaskPrioritizationChain.from_llm(\n",
        "            llm, verbose=verbose\n",
        "        )\n",
        "        llm_chain = LLMChain(llm=llm, prompt=prompt)\n",
        "        tool_names = [tool.name for tool in tools]\n",
        "        agent = ZeroShotAgent(llm_chain=llm_chain, allowed_tools=tool_names)\n",
        "        agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)\n",
        "        return cls(\n",
        "            task_creation_chain=task_creation_chain,\n",
        "            task_prioritization_chain=task_prioritization_chain,\n",
        "            execution_chain=agent_executor,\n",
        "            vectorstore=vectorstore,\n",
        "            **kwargs\n",
        "        )"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "X7LjfUoY3pIk"
      },
      "source": [
        "## Run baby"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 18,
      "metadata": {
        "id": "w-Jf-Ggw3oBQ"
      },
      "outputs": [],
      "source": [
        "\n",
        "OBJECTIVE = \"Find the cheapest price and site to buy a Yubikey 5c online and give me the URL\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 19,
      "metadata": {
        "id": "uKceUoD43tUj"
      },
      "outputs": [],
      "source": [
        "llm = OpenAI(temperature=0)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 23,
      "metadata": {
        "id": "rVuhhl523wNa"
      },
      "outputs": [],
      "source": [
        "# Logging of LLMChains\n",
        "verbose=False\n",
        "# If None, will keep on going forever\n",
        "max_iterations: Optional[int] = 2\n",
        "baby_agi = BabyAGI.from_llm(\n",
        "    llm=llm,\n",
        "    vectorstore=vectorstore,\n",
        "    verbose=verbose,\n",
        "    max_iterations=max_iterations\n",
        ")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "r9FJ0vX93zRX",
        "outputId": "a3759701-dc25-4abf-afe0-e6a59557dd1b"
      },
      "outputs": [],
      "source": [
        "baby_agi({\"objective\": OBJECTIVE})"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "r0FVPi3e4Itk",
        "outputId": "aadb7286-30a3-4479-f02e-34e726c8a3ae"
      },
      "outputs": [],
      "source": [
        "\n",
        "OBJECTIVE = \"Find the of a Yubikey 5c on Amazon and give me the URL\"\n",
        "\n",
        "baby_agi({\"objective\": OBJECTIVE})"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "-_G2jIps5Z2v"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.4"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
